package com.qf.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qf.pojo.DtsStorage;
import com.qf.service.DtsStorageService;
import com.qf.util.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;

/**
 * @author 千锋教育
 * @Company http://www.mobiletrain.org/
 * @Version 1.0
 */
@RestController
@RequestMapping("/admin/storage")
public class DtsAdminStorageController {

    @Autowired
    DtsStorageService storageService;


    @RequestMapping("/delete")
    public Object delete(@RequestBody DtsStorage storage){
        storageService.delete(storage);
        return ResponseUtil.ok();
    }

    @RequestMapping("/update")
    public Object update(@RequestBody DtsStorage storage){
        DtsStorage storage1 = storageService.update(storage);
        return ResponseUtil.ok(storage1);
    }




    @GetMapping("/list")
    public Object list(@RequestParam(value = "page", required = false , defaultValue = "1") Integer page ,
                       @RequestParam(value = "limit", required = false , defaultValue = "10") Integer limit ,
                       String name ,String key, String sort, String order){
        IPage<DtsStorage> ipage = storageService.queryPage(page, limit, name ,key , sort, order);

        Map<String ,Object> data = new HashMap<>();
        data.put("total", ipage.getTotal() );
        data.put("items", ipage.getRecords());

        return ResponseUtil.ok(data);
    }

    @PostMapping("/create")
    public Object upload(MultipartFile file) throws Exception {
        String filename = file.getOriginalFilename();
        byte[] bytes = file.getBytes();
        Object upload = storageService.upload(filename, bytes, (int) file.getSize());
        return ResponseUtil.ok(upload);
    }
}
